<?php
namespace Admin\Controller;
use Think\Controller;
/**
* CacheController
*/
class CacheController extends CommonController
{
	//缓存设置
	public function index()
	{
		if (IS_POST) {
			$data = I('post.');
			$data['HOME_HTML_CACHE_ON'] = isset($data['HOME_HTML_CACHE_ON']) ? $data['HOME_HTML_CACHE_ON'] : 0;
			$data['HOME_INDEX_CACHE_ON'] = isset($data['HOME_INDEX_CACHE_ON']) ? $data['HOME_INDEX_CACHE_ON'] : 0;
			$data['HOME_LIST_CACHE_ON'] = isset($data['HOME_LIST_CACHE_ON']) ? $data['HOME_LIST_CACHE_ON'] : 0;
			$data['HOME_SHOW_CACHE_ON'] = isset($data['HOME_SHOW_CACHE_ON']) ? $data['HOME_SHOW_CACHE_ON'] : 0;
			$data['MOBILE_HTML_CACHE_ON'] = isset($data['MOBILE_HTML_CACHE_ON']) ? $data['MOBILE_HTML_CACHE_ON'] : 0;
			$data['MOBILE_INDEX_CACHE_ON'] = isset($data['MOBILE_INDEX_CACHE_ON']) ? $data['MOBILE_INDEX_CACHE_ON'] : 0;
			$data['MOBILE_LIST_CACHE_ON'] = isset($data['MOBILE_LIST_CACHE_ON']) ? $data['MOBILE_LIST_CACHE_ON'] : 0;
			$data['MOBILE_SHOW_CACHE_ON'] = isset($data['MOBILE_SHOW_CACHE_ON']) ? $data['MOBILE_SHOW_CACHE_ON'] : 0;
			foreach ($data as $key => $v) {
				$ret = M('cache')->where('name="'.$key.'"')->setField('value',$v);
			}
			if ($ret !== false) {
				cachefile($data);
				alertMes('修改成功','/admin/cache');
			} else {
				alertMes('修改失败','/admin/cache');
			}
		} else {
			$list = M('cache')->select();

			$vo   = array();
			foreach ($list as $k => $v) {
				$vo[$v['name']] = $v['value'];
			}
			$this->assign('vo', $vo);
			$this->display();
		}
	}
	//更新全站
	public function update($del=false)
	{
		if ($del) {
			$this->deldir("www");
			$this->deldir("m");
			$this->makehome($_SERVER['HTTP_HOST']);
			$this->makelist($_SERVER['HTTP_HOST']);
			$this->makeshow($_SERVER['HTTP_HOST']);
			alertMes('更新成功','/admin/cache/update');
		} else {
			$this->display();
		}
	}
	//更新URL
	public function updateUrl($mode = false)
	{
		if ($mode) {
			$list = M('category')->where('modelid!=0')->field('cateid,modelid,pathname,pagesize')->select();
			foreach ($list as $key => $value) {
				$arr['cate_url'] = "/list/index?pathname={$value['pathname']}&cateid={$value['cateid']}";
				M('category')->where('cateid='.$value['cateid'].' AND is_outer=0')->save($arr);
			}
			$table = M('model')->getField('table',true);
			foreach ($table as $key2 => $value2) {
				$tablename = 'model_'.$value2;
				$clist = M($tablename)
				->field("xwcms_{$tablename}.id,xwcms_category.pathname")
				->join('xwcms_category ON xwcms_'.$tablename.'.catid = xwcms_category.cateid')
				->select();
				foreach ($clist as $key1 => $value1) {
					$arr['url'] = "/show/index?pathname={$value1['pathname']}&id={$value1['id']}";
					M($tablename)->where('id='.$value1['id'])->save($arr);
				}
			}
		} else {
			$suffix = M('cache')->where("name='HTML_FILE_SUFFIX'")->getfield('value');
			$list = M('category')->getField('cateid,pathname',true);
			$depr = M('cache')->where("name='HOME_URL_PATHINFO_DEPR'")->getField('value');
			foreach ($list as $key => $value) {
				$arr['cate_url'] = "/{$value}/index{$suffix}";
				M('category')->where('cateid='.$key.' AND is_outer=0')->save($arr);
			}
			$table = M('model')->getField('table',true);
			foreach ($table as $key2 => $value2) {
				$tablename = 'model_'.$value2;
				$clist = M($tablename)
				->field("xwcms_{$tablename}.id,xwcms_category.pathname")
				->join('xwcms_category ON xwcms_'.$tablename.'.catid = xwcms_category.cateid')
				->select();
				foreach ($clist as $key1 => $value1) {
					$arr['url'] = "/{$value1['pathname']}/Show{$depr}{$value1['id']}{$suffix}";
					M($tablename)->where('id='.$value1['id'])->save($arr);
				}
			}
		}
		alertMes('更新成功','/admin/cache/update');
	}
	//清空静态页面
	protected function deldir($path)
	{
		if (file_exists($path)) {
			$handle = opendir($path);
			while (false !== ( $item = readdir($handle) )) {
				if ($item != "." && $item != "..")
					is_dir("$path/$item") ? $this->deldir("$path/$item") : unlink("$path/$item");
			}
			closedir($handle);
			rmdir($path);
		}
	}
	//生成首页
	protected function makehome($url)
	{
		ini_set('max_execution_time','0');
		file_get_contents("http://$url/");
		file_get_contents("http://$url/mobile");
	}
	//生成栏目列表
	protected function makelist($url)
	{
		ini_set('max_execution_time','0');
		$list = M('category')->where('modelid!=0')->field('cateid,modelid,pathname,pagesize')->select();
		foreach ($list as $key => $value) {
			$table = M('model')->where('modelid='.$value['modelid'])->getfield('table');
			$tablename = 'model_'.$table;
			$page = ceil((M($tablename)->where('catid='.$value['cateid'])->count())/$value['pagesize']);
			if ($page <= 1) {
				file_get_contents("http://$url/home/list/index?pathname={$value['pathname']}&cateid={$value['cateid']}");
				file_get_contents("http://$url/mobile/list/index?pathname={$value['pathname']}&cateid={$value['cateid']}");
			} else {
				for ($i=1; $i <= $page; $i++) {
					if ($i == 1) {
						file_get_contents("http://$url/home/list/index?pathname={$value['pathname']}&cateid={$value['cateid']}");
						file_get_contents("http://$url/mobile/list/index?pathname={$value['pathname']}&cateid={$value['cateid']}");
					} else {
						file_get_contents("http://$url/home/list/lists?pathname={$value['pathname']}&cateid={$value['cateid']}&page=$i");
						file_get_contents("http://$url/mobile/list/lists?pathname={$value['pathname']}&cateid={$value['cateid']}&page=$i");
					}
				}
			}
		}
	}
	//生成内容页
	protected function makeshow($url)
	{
		ini_set('max_execution_time','0');
		$table = M('model')->getField('table',true);
		foreach ($table as $key => $value) {
			$tablename = 'model_'.$value;
			$list = M($tablename)
			->field("xwcms_{$tablename}.id,xwcms_category.pathname")
			->join('xwcms_category ON xwcms_'.$tablename.'.catid = xwcms_category.cateid')
			->select();
			foreach ($list as $key1 => $value1) {
				file_get_contents("http://$url/home/show/index?pathname={$value1['pathname']}&id={$value1['id']}");
				file_get_contents("http://$url/mobile/show/index?pathname={$value1['pathname']}&id={$value1['id']}");
			}
		}
	}
}